package com.xhs.utils;

import com.xhs.exception.CommonException;
import com.xhs.message.Result;

/**
 * @desc: 异常工具类
 * @projectName: spring-boot-demo
 * @author: xhs
 * @date: 2025-08-20 周三 16:02
 */
public class ThrowUtil {

    /**
     * 条件成立则抛异常
     *
     * @param condition        条件
     * @param runtimeException 异常
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException) {
        if (condition) {
            throw runtimeException;
        }
    }

    /**
     * 条件成立则抛异常，并执行回调
     *
     * @param condition        条件
     * @param runtimeException 异常
     * @param callback         回调函数（可选）
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException, Runnable callback) {
        if (condition) {
            if (callback != null) {
                callback.run();
            }
            throw runtimeException;
        }
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition 条件
     * @param errorCode 错误码
     */
    public static void throwIf(boolean condition, Result errorCode) {
        throwIf(condition, new CommonException(errorCode));
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition 条件
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static void throwIf(boolean condition, Result errorCode, String message) {
        throwIf(condition, new CommonException(errorCode, message));
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition 条件
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static void throwIf(boolean condition, Result errorCode, String message,Object... arguments) {
        throwIf(condition, new CommonException(errorCode, message,arguments));
    }
}
